package com.kuang.demo01;

/**
 * Author: Zhang Dongwei
 * Date: 2022/3/13 10:06
 */
public class SaleTicketDemo02 {
    public static void main(String[] args) {
        // 并发：多线程操作同意资源类, 把资源类丢入线程类

        Ticket ticket = new Ticket();

        // lambda表达式:(参数)->{ 代码 }
        new Thread(()->{
            for(int i = 1; i < 40; i++){
                ticket.sale();
            }
        }, "A").start();
        new Thread(()->{
            for(int i = 1; i < 40; i++){
                ticket.sale();
            }
        }, "B").start();
        new Thread(()->{
            for(int i = 1; i < 40; i++){
                ticket.sale();
            }
        }, "C").start();

    }
}

// 资源类 OOP
class Ticket{
    // 属性、方法
    private int number = 30;

    // 卖票的方式
    // synchroized本质：队列，锁
    public synchronized void sale(){
        if(number>0){
            System.out.println(Thread.currentThread().getName()+"卖出了"+(number--)+"票，剩余："+number);
        }
    }
}
